<template>
    <div class="ps-rater">
        <!--评分组件-->
        <input v-model="currentValue" style="display:none">
        <a class="ps-rater-box" v-for="i in max" @click="handleClick(i-1)"
                :class="{'is-active': currentValue > i-1}"
                :style="{color: colors && colors[i-1] ? colors[i-1] : '#ccc', marginRight: margin+'px',fontSize: fontSize+'px',width: fontSize + 'px', height: fontSize + 'px', lineHeight: fontSize + 'px'}">
            <p class="ps-rater-inner">{{star}}
                <span class="ps-rater-outer" :style="{color: activeColor, width: cutPercent+'%'}" v-if="cutPercent > 0 && cutIndex === i-1">{{star}}</span>
            </p>
        </a>
    </div>
</template>

<script>
export default {
  props: {
    max: {
      type: Number,
      default: 5
    },
    value: {
      type: Number,
      default: 0
    },
    disabled: Boolean,
    star: {
      type: String,
      default: '★'
    },
    activeColor: {
      type: String,
      default: '#fc6'
    },
    margin: {
      type: Number,
      default: 2
    },
    fontSize: {
      type: Number,
      default: 25
    }
  },
  data () {
    return {
      colors: [],
      currentValue: 0
    }
  },
  computed: {
    sliceValue () {
      const _val = this.currentValue.toString().split('.')
      return _val.length === 1 ? [_val[0], 0] : _val
    },
    cutIndex () {
      return this.sliceValue[0] * 1
    },
    cutPercent () {
      return this.sliceValue[1] * 10
    }
  },
  methods: {
    handleClick (i, force) {
        if (!this.disabled || force) {
            if (this.currentValue === i + 1) {
                this.currentValue = i
                this.updateStyle()
            } else {
                this.currentValue = i + 1
            }
        }
    },
    updateStyle () {
        for (let j = 0; j < this.max; j++) {
            if (j <= this.currentValue - 1) {
                this.$set(this.colors, j, this.activeColor)
            } else {
                this.$set(this.colors, j, '#ccc')
            }
        }
    }
  },
  watch: {
    currentValue (val) {
      this.updateStyle()
      this.$emit('input', val)
    },
    value (val) {
      this.currentValue = val
    }
  },
  created () {
    this.currentValue = this.value
  },
  mounted () {
    this.updateStyle()
  },
}
</script>

<style lang="less" scoped>
    .ps-rater {
      text-align: left;
      display: inline-block;
      line-height: normal;
    }
    .ps-rater a {
      display: inline-block;
      text-align: center;
      cursor: pointer;
      color: #ccc;
    }
    .ps-rater a:last-child {
      padding-right: 2px!important;
      margin-right: 0px!important;
    }
    .ps-rater a:hover {
      color: #ffdd99;
    }
    .ps-rater a.is-disabled {
      color: #ccc !important;
      cursor: not-allowed;
    }
    .ps-rater-box {
      position: relative;
    }
    .ps-rater-inner {
      position: relative;
      display: inline-block;
    }
    .ps-rater-outer {
      position: absolute;
      left: 0;
      top: 0;
      display: inline-block;
      overflow: hidden;
    }
</style>